import { ref } from 'vue'
import { adapter, request } from '@/request'
import type { PatternEnum } from '@/views/word-lib/interface'
import { bannedWordByGet } from '@/core/api/word'
import type { Pagination, RequestWrapper, WithTotalSize } from '@/core/interfaces/common'

interface Params extends Pagination {
  industryId?: number
  word?: string
  libId?: number
}

export interface BannedWord {
  industryCode: number
  industryName: string
  industryId: number
  createTime: string
  creator: number
  id: number
  matchStyle: PatternEnum
  libId: number
  libName: string
  word: string
}

export default function getBannedWord() {
  const service = adapter<RequestWrapper<WithTotalSize<Record<'words', BannedWord[]>>>>(request)
  const loading = ref(false)
  const response = ref<WithTotalSize<Record<'words', BannedWord[]>>>({
    totalSize: 0,
    words: []
  })

  const fetch = (params: Params) => {
    const { url, method } = bannedWordByGet()
    loading.value = true

    return service({
      config: {
        url,
        method,
        params
      },
      callback: (res) => {
        response.value = res.data.data
      },
      error: (err) => {
        console.log(err)
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [response, fetch, loading] as const
}
